package com.aier.entrance.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.entrance.entity.EntranceDevice;
import com.aier.entrance.entity.EntranceDeviceRowMapper;
import com.aier.entrance.entity.VideoDevice;
import com.aier.entrance.entity.VideoDeviceRowMapper;
import com.aier.entrance.service.IEntranceInitCommonService;
import com.aier.entrance.service.IVideoDeviceService;
import com.aier.proxy.entity.VideoDeviceInfo;
import com.aier.proxy.entity.mapper.VideoDeviceInfoRowMapper;
import com.aier.proxy.service.IProxyService;

@Component("videoDeviceService")
public class VideoDeviceServiceImpl_J implements IVideoDeviceService {

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;
    
    @Resource(name="entranceInitCommonService")
    private IEntranceInitCommonService entranceInitCommonService;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }
    public void setEntranceInitCommonService(
            IEntranceInitCommonService entranceInitCommonService) {
        this.entranceInitCommonService = entranceInitCommonService;
    }

    @Override
    public VideoDevice searchVideoDevice(Long vdid) {
        return (VideoDevice) commonDao.listByObject(
                "select * from video_device where vid=?",
                new Object[] { vdid }, new VideoDeviceRowMapper());
    }

    @Override
    public Map<String, Object> searchProxyVideo(int page, int page_size,
            Long uid) {
        return commonDao.queryPage("select * from video_device as vd LEFT JOIN proxy_user as pu " +
                "on pu.pid=vd.pid LEFT JOIN `user` as u on u.uid=pu.uid where u.uid=?",
                page, page_size, new Object[] { uid },
                new VideoDeviceRowMapper());
    }
    
    @Override
    public Map<String, Object> searchProxyVideoInfo(int page, int page_size,
            Integer pid, String sn, Short state, Short supplier, int sortable) {
        String sql = "select * from video_device vd where vd.pid=? and vd.state != -1";
        if (sn!=null&&!sn.equals("")){
            sql += " and vd.sn ='" + sn + "'";
        } 
        if (state!= null && state != -1){
            sql += " and vd.state ='" + state + "'";
        } 
        if (supplier!= null && supplier != -1){
            sql += " and vd.supplier ='" + supplier + "'";
        }
        switch (sortable) {
            case IProxyService.ENTRANCE_SORTABLE_SN:
                sql += " order by vd.sn";
                break;
            case IProxyService.ENTRANCE_SORTABLE_SN_DESC:
                sql += " order by vd.sn desc";
                break;
        }
        
        return commonDao.queryPage(sql,
                page, page_size, new Object[] { pid},
                new VideoDeviceInfoRowMapper());
    }

    @Override
    public VideoDeviceInfo searchByvid(Long vid) {
        return (VideoDeviceInfo) commonDao.listByObject(
                "select * from video_device where vid=?", new Object[] { vid },
                new VideoDeviceInfoRowMapper());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<EntranceDevice> searchEntranceByUid(Integer pid) {
        return (List<EntranceDevice>) commonDao.list("select * from entrance_device as ed where ed.pid=?", new Object[] { pid },
                new EntranceDeviceRowMapper());
    }

    @Override
    public int updateVideoByvid(Long vid, Short state, String user, String pwd) {
        
        if (commonDao
                .update("update video_device v set v.state=?,v.user=?,v.pwd=? where v.vid=?",
                        new Object[] { state, user, pwd, vid }) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int updateVideoState(Long vid) {
        if (commonDao
                .update("update video_device v set v.state=-10 where v.vid=?",
                        new Object[] { vid }) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int updateVideoEntrance(Long eid, Long vid) {
        
        if (commonDao
                .update("update entrance_device e set e.vdid=? where e.eid=?",
                        new Object[] { vid,eid }) > 0) {
            entranceInitCommonService.initEntrance();
            return 1;
        } else {
            return 0;
        }
    }

}
